package com.lifesting.front;

import com.sun.facelets.FaceletContext;
import com.sun.facelets.tag.MetaRule;
import com.sun.facelets.tag.Metadata;
import com.sun.facelets.tag.MetadataTarget;
import com.sun.facelets.tag.TagAttribute;

public class TableRule extends MetaRule {
	public static class RowCheckableMetaData extends Metadata{
		private String value;
		public RowCheckableMetaData(String value) {
			super();
			this.value = value;
		}
		@Override
		public void applyMetadata(FaceletContext ctx, Object instance) {
			((Table)instance).setRowsCheckable(Boolean.valueOf(value));	
		}
	}
	public static class IdFieldMetaData extends Metadata{
		private String value;
		public IdFieldMetaData(String value) {
			super();
			this.value = value;
		}
		@Override
		public void applyMetadata(FaceletContext ctx, Object instance) {
			((Table)instance).setIdField(value);	
		}
	}
	public static class BeanNameMetaData extends Metadata{
		private String value;
		public BeanNameMetaData(String value) {
			super();
			this.value = value;
		}
		@Override
		public void applyMetadata(FaceletContext ctx, Object instance) {
			((Table)instance).setBeanName(value);
		}
	}
	@Override
	public Metadata applyRule(String name, TagAttribute attribute,
			MetadataTarget meta) {
		if (meta.isTargetInstanceOf(Table.class))
		{
			if ("checkable".equals(name)){
				return new RowCheckableMetaData(attribute.getValue());
			}
			else if ("idField".equals(name))
			{
				return new IdFieldMetaData(attribute.getValue());
			}
			else if ("bean".equals(name))
			{
				return new BeanNameMetaData(attribute.getValue());
			}
		}
		return null;
	}

}
